package leetcode.editor.cn;

public class Solution109 {
    public TreeNode sortedListToBST(ListNode head) {
            return helper(head, null);

    }

    public TreeNode helper(ListNode head, ListNode tail){
        if(head == tail){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != tail && fast.next != tail){
            fast = fast.next.next;
            slow = slow.next;
        }
        TreeNode root = new TreeNode(slow.val);
        root.left = helper(head,slow);
        root.right = helper(slow.next,tail);
        return root;
    }
}
